

import 'package:syria_sports/data/models/error_model.dart';

import '../../core/utils/pref_utils.dart';
import '../../core/utils/show_toast.dart';
import '../../presentation/coach_notes_page/models/note_model.dart';
import '../../presentation/exercises/exercises_page/models/exercise.dart';
import '../apiClient/api_client.dart';
import '../apiClient/api_keys.dart';
import '../models/data.dart';
import '../remote/dio_helper.dart';

class LinksController{
  static Future<Data?> getAllLinks({bool isPublic = false})async{

    var res = await DioHelper.getData(
        url:isPublic? ApiClient.getPublicLinks: ApiClient.getLink,
        bearerToken:isPublic? null: PrefUtils.getToken(),
    );
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      // showMessage(res.data[ApiKeys.message]);
    }
    Data response = Data.fromJson(res.data);
    return response;
  }


  static Future<Data?> addNote(NoteModel note)async{
    var res = await DioHelper.postData(
      url: ApiClient.link,
      data: note.toJson(),
      bearerToken: PrefUtils.getToken(),
    );

    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }

  static Future<Data?> getNoteById(int id)async{
    var res = await DioHelper.getData(
      url: ApiClient.link,
      query: {
        'id':id
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else if(!res.data[ApiKeys.state]){
      showMessage(res.data[ApiKeys.message]);
    }else{
      Data response = Data.fromJson(res.data);
      return response;
    }
    return null;
  }

  static Future<Data?> deleteById(String id)async{
    var res = await DioHelper.postData(
      url: ApiClient.linkDelete,
      query: {
        'id':id
      },
      data: {
        'id':id,
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }

    return null;
  }

}